﻿Public Class frmEmployeeInfoDE
    Dim validator As PropertyValidator = New PropertyValidator
    Dim empInfoMgr As New EmployeeInfoManager
    Dim payrollMgr As New PayrollManager
    Dim EmpInfo As EmployeeInfo
    Dim dMgr As New DesignationManager


    Sub New(ByVal data As EmployeeInfo)

        ' This call is required by the designer.
        InitializeComponent()
        EmpInfo = data
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub frmEmployeeDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        validator.ResetControls(grpEmployeeInfoDE)
        If Not EmpInfo Is Nothing Then


            txtEmployeeNo.Text = EmpInfo.employee_no
            txtLastName.Text = EmpInfo.last_name
            txtFirstName.Text = EmpInfo.first_name
            txtMiddleName.Text = EmpInfo.middle_name
            txtSSSNo.Text = EmpInfo.no_sss
            txtPHINo.Text = EmpInfo.no_philhealth
            txtPagIBIGNo.Text = EmpInfo.no_pagibig
            txtTINNo.Text = EmpInfo.no_tin

            Dim tcArr As List(Of TaxCode) = payrollMgr.getAllTaxCode()
            For Each tc As TaxCode In tcArr
                cboTaxCode.Items.Add(tc.tax_code)
            Next

            If EmpInfo.tax_code.Length > 0 Then

                Dim tcArr2 As List(Of TaxCode) = payrollMgr.getItemTaxCodeByID(EmpInfo.tax_code)
                For Each tc2 As TaxCode In tcArr2
                    cboTaxCode.Text = tc2.tax_code
                    Exit For
                Next

            End If

            Dim dArr As List(Of Designation) = dMgr.getAllDesignation()
            For Each d As Designation In dArr
                cboDesignation.Items.Add(d.title)
            Next

            If EmpInfo.designation_id.ToString.Length > 0 Then
                Dim dArr2 As Designation = dMgr.retrieveDesignationById(EmpInfo.designation_id)
                cboDesignation.Text = dArr2.title
            End If

            dtpDateHired.Value = EmpInfo.date_hired
            dtpEndContract.Value = EmpInfo.end_contract
            dtpDateResigned.Value = EmpInfo.date_resigned
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEmptyString(txtEmployeeNo, "Employee No") Then Return
        If validator.IsEmptyString(txtLastName, "Last Name") Then Return
        If validator.IsEmptyString(txtFirstName, "First Name") Then Return
        If validator.IsEmptyString(txtMiddleName, "Middle Name") Then Return

        EmpInfo.employee_no = txtEmployeeNo.Text
        EmpInfo.last_name = txtLastName.Text
        EmpInfo.first_name = txtFirstName.Text
        EmpInfo.middle_name = txtMiddleName.Text
        EmpInfo.no_sss = txtSSSNo.Text
        EmpInfo.no_philhealth = txtPHINo.Text
        EmpInfo.no_pagibig = txtPagIBIGNo.Text
        EmpInfo.no_tin = txtTINNo.Text
        EmpInfo.tax_code = cboTaxCode.SelectedIndex.ToString
        'EmpInfo.designation_id = dMgr.retrieveDesignationByTitle(cboDesignation.SelectedItem.ToString).id
        EmpInfo.designation_id = cboDesignation.SelectedIndex.ToString
        EmpInfo.date_hired = dtpDateHired.Value.ToString("yyyy-M-d")
        EmpInfo.end_contract = dtpEndContract.Value.ToString("yyyy-M-d")
        EmpInfo.date_resigned = dtpDateResigned.Value.ToString("yyyy-M-d")

        empInfoMgr.updateEmployeeInfo(EmpInfo)
        MsgBox("Successfully updated.")
        Me.Close()

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        validator.ResetControls(grpEmployeeInfoDE)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpEmployeeInfoDE)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function

    Private Sub btnClear_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        validator.ResetControls(grpEmployeeInfoDE)
    End Sub
End Class